import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.sql.*;

@WebServlet("/AddUserServlet")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        response.sendRedirect("user_add.jsp");

        String loginName = request.getParameter("loginName");
        String password = request.getParameter("password");
        String realName = request.getParameter("realName");
        String email = request.getParameter("email");
        String userRole = request.getParameter("userRole");
        String userStatus = request.getParameter("userStatus");
        // 连接数据库
        String jdbcUrl = "jdbc:mysql://localhost:3306/jianggweb";
        String dbUser = "root";
        String dbPassword = "123456";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);

            // 插入数据到用户表
            String sql = "INSERT INTO users (login_name,password,real_name,email,user_role,user_status) VALUES (?, ?, ?, ?, ?, ?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, loginName);
            statement.setString(2, password);
            statement.setString(3, realName);
            statement.setString(4, email);
            statement.setString(5, userRole);
            statement.setString(6, userStatus);
            // 设置其他字段的值...
            statement.executeUpdate();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}